﻿using System;
using System.ComponentModel;
using System.Web.UI;
using System.Collections;
using System.Collections.Generic;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.Core;
using System.Collections.Specialized;
using System.Web;

namespace Yuanlin.Business
{
    public class AddComment : YuanlinUserControl
    {
        protected HtmlAnchor endpageUrl;
        protected Literal countLtl;
        protected HtmlControl listDiv;
        protected HiddenField isRefresh;
        protected ValuedTextBox contentValuedTextBox;
        protected ResourceButton saveButton;
        private int newsEntityType = 0;
        private YuanlinUser user = HttpContext.Current.User as YuanlinUser;
        private int newsId = UrlManager.Instance.GetParameterValue<int>(YuanlinParameterName.NewsId);
        private int companyNewsId = UrlManager.Instance.GetParameterValue<int>(YuanlinParameterName.CompanyNewsId);
        private string greenNum = UrlManager.Instance.GetParameterValue<string>(YuanlinParameterName.GreenNum);

        public string NewsEntityType
        {
            get { return ViewState["NewsEntityType"] == null ? "" : ViewState["NewsEntityType"].ToString(); }
            set { ViewState["NewsEntityType"] = value; }
        }

        protected override void OnLoad(EventArgs e)
        {
            if (NewsEntityType == "News")
            {
                newsEntityType = (int)CategoryType.News;
            }
            else if (NewsEntityType == "CompanyNews")
            {
                newsEntityType = (int)CategoryType.CompanyNews;
            }

            if (!Page.IsPostBack)
            {
                BindData();
            }
            else
            {
                if (!ValidateAuthCode(HttpContext.Current.Request.Form))
                {
                    Globals.ShowMessage(Page, "AuthCodeError", "验证码输入不正确！");
                    return;
                }
            }
            saveButton.Click += new EventHandler(saveButton_Click);
        }

        private void BindData()
        {
            if (user.IsAnonymous)//登入后才能发表评论
            {
                saveButton.Enabled = false;
                saveButton.Text = ResourceManager.GetString("NoLogin");
            }
            else
            {
                saveButton.Text = ResourceManager.GetString("AddComment");
            }
            CommentListRequest request = new CommentListRequest();
            if (newsEntityType == (int)CategoryType.News)
            {
                request.NewsId = newsId;
            }
            else if (newsEntityType == (int)CategoryType.CompanyNews)
            {
                request.NewsId = companyNewsId;
            }
            request.NewsEntityType = newsEntityType;
            request.State = 0;
            request.PageIndex = 0;
            request.PageSize = 10000;
            EntityList comments = BusinessManager.GetCommentList(RequestHelper.BuildGetCommentListRequest(request));
            if (comments.Count == 0)
            {
                listDiv.Visible = false;
            }
            else
            {
                countLtl.Text = comments.Count.ToString();
                listDiv.Visible = true;
            }

            endpageUrl.HRef = GetUrl(newsEntityType);
        }
        private string GetUrl(int newsEntityType)
        {
            if (newsEntityType == (int)CategoryType.News)
            {
                return SiteUrls.Instance().GetNewsCommentListUrl(greenNum, newsId);
            }
            else if (newsEntityType == (int)CategoryType.CompanyNews)
            {
                return SiteUrls.Instance().GetCompanyNewsCommentListUrl(greenNum, companyNewsId);
            }
            return "";
        }
        private bool ValidateAuthCode(NameValueCollection formData)
        {
            if (HttpContext.Current.Request.Cookies["AuthCode"] != null &&
                HttpContext.Current.Request.Cookies["AuthCode"].Value != null &&
                HttpContext.Current.Request.Cookies["AuthCode"].Value == formData["number"])
            {
                return true;
            }
            return false;
        }

        private void saveButton_Click(object sender, EventArgs e)
        {
            Comment comment = new Comment();

            comment.Content.Value = CensorWordManager.CensorWord(this.contentValuedTextBox.Value);
            if (newsEntityType == (int)CategoryType.News)
            {
                comment.NewsId.Value = newsId;
            }
            else if (newsEntityType == (int)CategoryType.CompanyNews)
            {
                comment.NewsId.Value = companyNewsId;
            }
            comment.NewsEntityType.Value = newsEntityType;
            comment.CreateDate.Value = DateTime.Now;
            comment.State.Value = 0;//0表示可用，1表示禁用
            comment.UserId.Value = user.EntityId.Value;
            comment.GreenNumber.Value = user.CurrentGreenNumber;

            BusinessManager.CreateComment(comment);

            Page.Response.Redirect(GetUrl(newsEntityType));
        }

        private bool _refreshState;
        private bool _isRefresh;

        public bool IsRefresh
        {
            get
            {
                return _isRefresh;
            }
        }
        protected override void LoadControlState(object savedState)
        {
            object[] allStates = (object[])savedState;
            base.LoadControlState(allStates[0]);
            _refreshState = (bool)allStates[1];
            _isRefresh = _refreshState == (bool)HttpContext.Current.Session["__ISREFRESH"];
        }
        protected override object SaveControlState()
        {
            HttpContext.Current.Session["__ISREFRESH"] = _refreshState;
            object[] allStates = new object[2];
            allStates[0] = base.SaveControlState();
            allStates[1] = !_refreshState;
            return allStates;
        }

    }
}
